package com.ssm.controller;

import com.ssm.pojo.Customer;
import com.ssm.pojo.Order;
import com.ssm.pojo.Quotation;
import com.ssm.pojo.User;
import com.ssm.service.OrderService;
import com.ssm.service.QuotationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.*;

@Controller
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private QuotationService quotationService;

    @RequestMapping("/list")
    public String list(){
        return "order/order";
    }

    @RequestMapping("/listdata")
    @ResponseBody
    public Map<String,Object> listData(HttpSession session,String orderNO,Short status,Integer page, Integer limit){

        List<Order> orders = new ArrayList<Order>();
        Integer count=0;
        Short type = (Short) session.getAttribute("type");
        Customer customer = null;
        User user = null;
        if(status == null){
            status =0;
        }
        if(status==-1){
            status=null;
        }
        if(type.shortValue()==2){

            user = (User)session.getAttribute("user");
            orders = orderService.selectOrdersPage(orderNO,null,status,page,limit);
            count = orderService.countSize(orderNO,null,status);
        }
        if(type.shortValue()==1){
            customer = (Customer) session.getAttribute("customer");
            orders = orderService.selectOrdersPage(orderNO,customer.getCustomerNO(),status,page,limit);
            count = orderService.countSize(orderNO,customer.getCustomerNO(),status);
        }

        // 返回表格数据
        Map<String,Object> resultMap = new HashMap<String, Object>();
        resultMap.put("code",0);
        resultMap.put("msg","");
        resultMap.put("count",count);
        resultMap.put("data",orders);
        return resultMap;
    }

    @RequestMapping(value = "quote",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,String> quote(HttpSession session,String orderNO, BigDecimal quoteprice, String otherinfo){
        Order order = orderService.selectByNO(orderNO);
        order.setQuoteprice(quoteprice);
        order.setOtherInfo(otherinfo);
        order.setStatus((short)1);
        orderService.update(order);

        Quotation quotation = new Quotation();
        quotation.setQuotationNO(System.currentTimeMillis()+"");
        quotation.setOrderNO(orderNO);
        User user = (User)session.getAttribute("user");
        if(user!=null){
            quotation.setQuotationMan(user.getUsername());
        }
        quotation.setQuotationTime(new Date());
        quotation.setOtherInfo(otherinfo);
        quotationService.insertQuo(quotation);
//        quotation.setProduct();

        Map<String,String> map = new HashMap<String, String>();
        map.put("status","true");
        map.put("msg","操作成功");
        return map;
    }

    @RequestMapping(value = "delete",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,String> delete(String orderNO){
        orderService.delete(orderNO);
        Map<String,String> map = new HashMap<String, String>();
        map.put("status","true");
        map.put("msp","操作成功");
        return map;
    }

    @RequestMapping(value = "confirm", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,String> update(String orderNO,Short status){
        Order order = new Order();
        order.setOrderNO(orderNO);
        order.setStatus(status);
        orderService.update(order);
        Map<String,String> map = new HashMap<String, String>();
        map.put("status","true");
        map.put("msg","操作成功");
        return map;
    }
}
